package com.carb.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.carb.entity.Item;
import com.carb.entity.Project;
import com.carb.enums.BusinessStatus;
import com.carb.enums.CommonStatus;
import com.carb.enums.UserRole;
import com.carb.mapper.BusinessMapper;
import com.carb.mapper.ItemMapper;
import com.carb.mapper.ProjectMapper;
import com.carb.mapper.UserMapper;
import com.carb.service.WorkSpaceService;
import com.carb.vo.BusinessDataVO;
import com.carb.vo.BusinessStatusDataVO;
import com.carb.vo.ItemStatusDataVO;
import com.carb.vo.ProjectStatusDataVO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

/**
 * <p>
 * 工作台 服务实现类
 * </p>
 *
 * @author XuChaoyang
 * @since 2025-02-12
 */
@Service
public class WorkSpaceServiceImpl implements WorkSpaceService {

    @Resource
    private BusinessMapper businessMapper;

    @Resource
    private UserMapper userMapper;

    @Resource
    private ProjectMapper projectMapper;

    @Resource
    private ItemMapper itemMapper;

    /**
     * 今日业务数据查询
     * @return 今日业务数据VO
     */
    @Override
    public BusinessDataVO getBusinessData() {
        // 1、今日的起止时间
        LocalDateTime beginTime = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
        LocalDateTime endTime = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);

        // 2、查询今日总营业额、业务总数、有效业务数、业务完成率、新增会员数、活跃会员数
        Double turnover = businessMapper.getSumTurnover(beginTime, endTime);
        turnover = turnover == null ? 0.0 : turnover;
        Integer totalBusinessCount = businessMapper.getBusinessCount(beginTime, endTime, null);
        Integer validBusinessCount = businessMapper.getBusinessCount(beginTime, endTime, BusinessStatus.COMPLETED);
        Double businessCompletionRate = totalBusinessCount == 0 ? 0.0 : validBusinessCount.doubleValue() / totalBusinessCount;
        Integer newMemberCount = userMapper.getUserCount(beginTime, endTime, UserRole.MEMBER);
        Integer activeUserCount = userMapper.getActiveUserCount(beginTime, endTime, UserRole.MEMBER);

        // 3、封装VO
        return BusinessDataVO.builder()
                .turnover(turnover)
                .totalBusinessCount(totalBusinessCount)
                .validBusinessCount(validBusinessCount)
                .businessCompletionRate(businessCompletionRate)
                .newMemberCount(newMemberCount)
                .activeMemberCount(activeUserCount)
                .build();
    }

    /**
     * 今日业务状态数据查询
     * @return 今日业务状态数据VO
     */
    @Override
    public BusinessStatusDataVO getBusinessStatusData() {
        // 1、今日的起止时间
        LocalDateTime beginTime = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
        LocalDateTime endTime = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);

        // 2、查询今日待接单业务数、正在服务业务数、已完成业务数、已退款业务数、总业务数
        Integer unacceptedBusinessCount = businessMapper.getBusinessCount(beginTime, endTime, BusinessStatus.UNACCEPTED);
        Integer onServiceBusinessCount = businessMapper.getBusinessCount(beginTime, endTime, BusinessStatus.IN_PROGRESS);
        Integer completedBusinessCount = businessMapper.getBusinessCount(beginTime, endTime, BusinessStatus.COMPLETED);
        Integer refundedBusinessCount = businessMapper.getBusinessCount(beginTime, endTime, BusinessStatus.REFUNDED);
        Integer totalBusinessCount = businessMapper.getBusinessCount(beginTime, endTime, null);

        // 3、封装VO
        return BusinessStatusDataVO.builder()
                .unacceptedBusinessCount(unacceptedBusinessCount)
                .onServiceBusinessCount(onServiceBusinessCount)
                .completedBusinessCount(completedBusinessCount)
                .refundedBusinessCount(refundedBusinessCount)
                .totalBusinessCount(totalBusinessCount)
                .build();
    }

    /**
     * 服务项目状态数据查询
     * @return 服务项目状态数据VO
     */
    @Override
    public ProjectStatusDataVO getProjectStatusData() {
        // 1、查询已启用、已禁用的服务项目数
        Long enabledProjectCount = projectMapper.selectCount(Wrappers.lambdaQuery(Project.class)
                .eq(Project::getStatus, CommonStatus.ENABLED));
        Long disabledProjectCount = projectMapper.selectCount(Wrappers.lambdaQuery(Project.class)
                .eq(Project::getStatus, CommonStatus.DISABLED));

        // 2、封装VO
        return ProjectStatusDataVO.builder()
                .enabledProjectCount(enabledProjectCount.intValue())
                .disabledProjectCount(disabledProjectCount.intValue())
                .build();
    }

    /**
     * 物品状态数据查询
     * @return 物品状态数据VO
     */
    @Override
    public ItemStatusDataVO getItemStatusData() {
        // 1、查询已启用、已禁用的物品数
        Long enabledItemCount = itemMapper.selectCount(Wrappers.lambdaQuery(Item.class)
                .eq(Item::getStatus, CommonStatus.ENABLED));
        Long disabledItemCount = itemMapper.selectCount(Wrappers.lambdaQuery(Item.class)
                .eq(Item::getStatus, CommonStatus.DISABLED));

        // 2、封装VO
        return ItemStatusDataVO.builder()
                .enabledItemCount(enabledItemCount.intValue())
                .disabledItemCount(disabledItemCount.intValue())
                .build();
    }

}
